home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
001
/
pn.arc
/
PN.PLB
next >
Wrap
Text File
|
1985-08-12
|
7KB
|
168 lines
{ PN.PLB #2.00 85-08-06 NORMAL PROBABILITY DISTRIBUTION FUNCTION
V02 L00 85-08-06 conversion to Turbo Pascal by Dennis E. Hamilton,
for separate testing for use as part of the Chi-Squared
Distribution procedure, Q2(x, df). This version is actually
a clone of ERF.PLB #2.00, since the functions are so similar.
V01 L01 79-03-22 implementated in Benton Harbor BASIC 05.01.01 by DEH;
used in deriving chi-squared distributions in testing RND().
L00 79-03-18 Texas Instruments SR52 calculator program derived
by Dennis E. Hamilton to verify basic recurrence methods and
checking against values in standard tables. }
function
PN(x: real {value of normally-distributed random variable} )
:real {approximate Normal Probability Distribution Function at x};
{Probability that, for Y a normally-distributed random variable
with mean 0 and standard deviation 1.0, Y will take on a value
not greater than x. 100*PN(x) is the percentile of the entire
Y population having value not greater than x. Similarly, QN(x),
defined to be 1-PN(x), is the probability with which Y > x. }
const
sqrt2 = 1.414213562373;
{an accurate-enough value of sqrt(2) from [HMF: Table 1.1]}
e1max = 1E-10;
{largest positive value of x for which exp(x) is exactly 1.0
to the floating-point precision of this computer. This value
is used to avoid computation of insignificant terms.}
e0min = 81;
{smallest nonzero value of x at which exp(-x) underflows to
0.0 within the computer's floating-point precision. This value
is used to steer around avoidable arithmetic overflows.}
var y: real {intermediate result};
function H1(x: real)
:real {approximation of 1-erf(abs(x)) where erf(x) is
the standard Gaussian error integral [HMF: 7.1.1]};
const a6 = 0.0000430638; a5 = 0.0002765672;
a4 = 0.0001520143; a3 = 0.0092705272;
a2 = 0.0422820123; a1 = 0.0705230784;
xmax = 9 {value of sqrt(e0min)};
begin {Computation to over 6 decimal-place precision based on Hastings
approximation 7.1.28 in [Abramowitz, Milton., Stegun, Irene A.
(eds.) HANDBOOK OF MATHEMATICAL FUNCTIONS. National Bureau of
Standards Applied Mathematics Series Publication #55. Dover
Publications (New York: 1965)]. For more about this function,
consult the description of companion function erf(x) in file
ERF.PLB #2.00. }
x := abs(x);
if x > xmax
then H1 := 0
else begin
x := (((((a6*x + a5)*x + a4)*x + a3)*x + a2)*x + a1)*x + 1.0;
H1 := sqr(sqr(sqr(sqr(1.0/x))));
end;
end {H1};
BEGIN {PN};
{Approximation of the Normal (Gaussian) Probability Distribution
Function based on standard relationships [HMF: section 26.2] and
availability of approximation H1(x) = 1 - erf(abs(x)).
To also employ erf(x) and H1(x) independent of PN(x) computation,
remove H1(x) from here, making it global and available for separate
usage by PN, erf, and any other functions that depend on it. }
y := H1(x/sqrt2)/2.0; {PN(-x) = 1 - PN(x), [HMF 26.2.5-2.6]}
if x < 0 { PN(x) = (1+erf(x/sqrt2))/2, [HMF 7.1.22] }
then PN := y { = 1 - H1(x/sqrt2)/2, x not negative }
else PN := 1.0 - y; { = H1(x/sqrt2)/2, x < 0 }
END {PN};
{ The following results were obtained with a test program (PNT1.PAS) using
test data file PNT1.DAT #1.00. PNT1 was compiled and executed using
Borland International Turbo Pascal 3.00A for CP/M-80. This version
maintains a 40-bit floating-point significand and decimal output
conversion rounded to 11 significant digits. The error terms represent
the absolute difference between computed values and those given in
known tables. In checking behavior at important extreme points, the
results of tests with ERF.PLB #2.00 were used to obtain trial values.
PNT1> #2.00 85-08-06 NORMAL PROBABILITY DISTRIBUTION FUNCTION
x PN(x) abs(error)
-1.273000000E+01 0.0000000000E+00 0.00E+00
-1.272000000E+01 1.8175366526E-28 1.82E-28
-9.013270000 4.5747132329E-18 4.47E-18
-7.348800000 2.9209847295E-13 1.92E-13
-6.706020000 1.7491994026E-11 7.49E-12
-5.997810000 1.2641232113E-09 2.64E-10
-5.199340000 0.00000010694 6.94E-09
-4.753420000 0.00000102818 2.82E-08
-4.264890000 0.00001008388 8.39E-08
-3.719020000 0.00010012594 1.26E-07
The values computed for PN(x), with x very small, are
compared with tabulated values of PN(-x) = QN(x) in The
Handbook of Mathematical Functions [HMF: Table 26.6].
Errors within 1E-5 are usually quite acceptable. Note
the very high relative errors out beyond -6.0, however.
z PN(x) abs(error)
-0.140000000 0.44433009345 9.82E-08
-0.020000000 0.49202174538 5.91E-08
-0.002510000 0.49899866454 1.34E-06
-3.647656264E-11 0.49999999999 1.32E-11
-3.647514842E-11 0.50000000000 0.00E+00
0.000000000E+00 0.50000000000 0.00E+00
3.647514842E-11 0.50000000000 0.00E+00
3.647656264E-11 0.50000000001 1.36E-11
0.002510000 0.50100133546 1.34E-06
0.020000000 0.50797825462 5.91E-08
0.140000000 0.55566990655 9.83E-08
Midrange values, near .5, check on the smoothness of
the crossover from negative to positive values of x.
Test values are from [HMF:Table 26.1] and ERFT1.PAS.
x PN(x) abs(error)
2.326350000 0.99000003622 3.62E-08
3.090230000 0.99900004431 4.43E-08
3.719020000 0.99989987406 1.26E-07
4.264890000 0.99998991612 8.39E-08
4.753420000 0.99999897182 2.82E-08
5.199340000 0.99999989307 6.93E-09
5.612000000 0.99999998857 1.43E-09
5.997810000 0.99999999874 2.62E-10
6.361340000 0.99999999986 4.37E-11
6.706020000 0.99999999998 6.37E-12
7.034480000 1.00000000000 0.00E+00
Near the tail, as PN(x) approaches 1.0, values of 1-QN(x)
from [HMF: Table 26.6] are used to see how well PN(x) does
despite adjustments by 1.0 within the PN procedure.
PNT1> End of test.
}
(* end of PN.PLB *)